<?php

namespace Ad\Logic\Shike;

use \common\Table\InputClick\AdClickShikeTable;
use \common\Table\InputClick\AdClickTable;
use \core\filter\filter;
use \common\Factory\Factory;
use \core\data\partitionTable;

class ShikeInputClick {
    
    public function insert($data) {
        Factory::getServer('MqWriteLog')->send('shike click raw input params:' . json_encode($data));
        //验证及过滤
        $adShikeClickColumns = array(
            'appid' => FILTER_SANITIZE_STRING,
            'idfa' => FILTER_SANITIZE_STRING, 
            'mac' => FILTER_SANITIZE_STRING,
            'udid' => FILTER_SANITIZE_STRING,
            'ip' => array(
                'filter' => FILTER_VALIDATE_IP,
            ),
            'os' => FILTER_SANITIZE_STRING,
            'callback' => FILTER_SANITIZE_ENCODED
        );
        $adClickColumns = array('idfa' => FILTER_SANITIZE_STRING);
        $filter = new filter();
        $data = array_change_key_case($data, CASE_LOWER);
        $adShikeClickColumns = $filter->varArray($data, $adShikeClickColumns);
        $adClickColumns = $filter->varArray($data, $adClickColumns);
        
        $result = false;
        if($adShikeClickColumns !== false && $adClickColumns !== false) {
            //合并额外参数
            $timestamp = time();
            $config = \core\init\init::getConfig();
            $adShikeClickColumns = array_merge($adShikeClickColumns, array('input_timestamp' => $timestamp));
            $adClickColumns = array_merge($adClickColumns, array('type' => $config['adType']['shike'], 'input_timestamp' => $timestamp));
            
            //入库
            $adClickShikeTable = new AdClickShikeTable();
            $db = $adClickShikeTable->getDb();
            
            $partitionTable = new partitionTable();
            $db->beginTransaction();
            $tableNum = $partitionTable->getHashTable($adShikeClickColumns['idfa']);
            $shikeResult = $adClickShikeTable->insertAdShikeClick($adShikeClickColumns, $tableNum);
            if($shikeResult === true) {
                $adClickTable = new AdClickTable();
                $result = $adClickTable->insertAdClick($adClickColumns);
                if($result === true) {
                    $db->commit();
                    Factory::getServer('MqWriteLog')->send('shike click shike_click table insert success');
                }
                else {
                    $db->rollBack();
                    Factory::getServer('MqWriteLog')->send('shike click ad_click table insert failed');
                }
            }
            else {
                Factory::getServer('MqWriteLog')->send('shike click shike_click table insert failed');
                $db->rollBack();
            }
        }
        else {
            Factory::getServer('MqWriteLog')->send('shike click raw input params validate failed');
        }
        
        if($result === true) {
            $result = array('success' => true, 'message' => 'ok');
        }
        else {
            Factory::getServer('MqWriteLog')->send('shike click failed');
            $result = array('success' => false, 'message' => '失败');
        }
        
        return $result;
    }
}